using Blazorise;
using Volo.Abp.AspNetCore.Components.Web.Extensibility.EntityActions;
using Volo.Abp.AspNetCore.Components.Web.Extensibility.TableColumns;
using Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars;

using WL.Think.Abp.Localization;
using WL.Think.Abp.OpenIddict.Dtos;
using WL.Think.Abp.OpenIddict.Permission;

namespace WL.Think.Abp.OpenIddict.Pages.Applications;

public partial class ApplicationManagement
{
    protected string? ResourceText { get; set; }
    protected PageToolbar Toolbar { get; } = new();
    protected List<TableColumn> DataColumns => TableColumns.Get<ApplicationManagement>();
    protected ApplicationManagerModal applicationManagerModal { get; set; }

    public ApplicationManagement()
    {
        CreatePolicyName = OpenIddictPermissions.Applications.Create;
        UpdatePolicyName = OpenIddictPermissions.Applications.Update;
        DeletePolicyName = OpenIddictPermissions.Applications.Delete;
        LocalizationResource = typeof(ThinkResource);
    }

    /// <summary>
    /// 继承父类，用来设置工具栏按钮
    /// </summary>
    /// <returns></returns>
    protected override ValueTask SetToolbarItemsAsync()
    {
        Toolbar.AddButton(L[ThinkResourceNames.New], OpenCreateModalAsync, IconName.Add,requiredPolicyName: CreatePolicyName);
        Toolbar.AddButton(L[ThinkResourceNames.Search], GetEntitiesAsync, IconName.Search, Color.Light);
        return base.SetToolbarItemsAsync();
    }


    protected override ValueTask SetBreadcrumbItemsAsync()
    {
        BreadcrumbItems.Add(new Volo.Abp.BlazoriseUI.BreadcrumbItem(L["OpenIddictManagement"].Value));
        BreadcrumbItems.Add(new Volo.Abp.BlazoriseUI.BreadcrumbItem(L["Applications"].Value));
        return base.SetBreadcrumbItemsAsync();
    }

    /// <summary>
    /// 继承父类，用来配置实体操作按钮 
    /// </summary>
    /// <returns></returns>
    protected override ValueTask SetEntityActionsAsync()
    {
        EntityActions
            .Get<ApplicationManagement>()
            .AddRange([
                new EntityAction
                {
                    Text = L[ThinkResourceNames.Edit],
                    Icon = "fa fa-pen-to-square",
                    Clicked = async (d) => await OpenEditModalAsync(d.As<ApplicationDto>()),
                    Visible = o => HasUpdatePermission
                },
                new EntityAction
                {
                    Text = L[ThinkResourceNames.Delete],
                    Icon = "fa fa-delete",
                    Clicked = async (d) => await DeleteEntityAsync(d.As<ApplicationDto>()),
                    Visible = o => HasDeletePermission,
                    ConfirmationMessage = (d) => GetDeleteConfirmationMessage(d.As<ApplicationDto>()),
                }
            ]);
        return base.SetEntityActionsAsync();
    }

    /// <summary>
    /// 继承父类，用来配置表格列
    /// </summary>
    /// <returns></returns>
    protected override async ValueTask SetTableColumnsAsync()
    {
        DataColumns
            .AddRange([
                new TableColumn
                {
                    Title = L[ThinkResourceNames.Action],
                    Actions = EntityActions.Get<ApplicationManagement>(),
                },
                new TableColumn{
                    Title = L[ThinkResourceNames.Name],
                    Data = nameof(ApplicationDto.ClientId),
                    PropertyName = nameof(ApplicationDto.ClientId),
                },
                new TableColumn{
                    Title = L[ThinkResourceNames.DisplayName],
                    Data = nameof(ApplicationDto.DisplayName),
                    PropertyName = nameof(ApplicationDto.DisplayName),
                },
                new TableColumn{
                    Title = L["ClientType"],
                    Data = nameof(ApplicationDto.ClientType),
                    PropertyName = nameof(ApplicationDto.ClientType)
                },
                new TableColumn{
                    Title = L["ApplicationType"],
                    Data = nameof(ApplicationDto.ApplicationType),
                    PropertyName = nameof(ApplicationDto.ApplicationType)
                },
                new TableColumn{
                    Title = L["GrantType"],
                    Data = nameof(ApplicationDto.GrantTypes),
                    PropertyName = nameof(ApplicationDto.GrantTypes),
                    ValueConverter = item => item.As<ApplicationDto>().GrantTypes.JoinAsString(",")
                }
            ]);

        await base.SetTableColumnsAsync();
    }

    /// <summary>
    /// 打开应用配置窗口
    /// </summary>
    /// <returns></returns>
    protected override async Task OpenCreateModalAsync()
    {
        await CheckCreatePolicyAsync();
        await applicationManagerModal.OpenAsync();
    }

    /// <summary>
    /// 打开应用编辑窗口
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    protected override async Task OpenEditModalAsync(ApplicationDto entity)
    {
        await CheckUpdatePolicyAsync();
        await applicationManagerModal.OpenAsync(entity.Id);
    }
}
